package com.zizy.healthexpert.Activity;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;

import com.zizy.healthexpert.Adapter.listNutritionAdapter;
import com.zizy.healthexpert.Database.MyDataBase_T_FOODS;
import com.zizy.healthexpert.Database.MyDataBase_T_FOODS_NUTRITION;
import com.zizy.healthexpert.Database.MyDataBase_T_FOODS_TYPE;
import com.zizy.healthexpert.Database.MyDataBase_T_NUTRITION;
import com.zizy.healthexpert.Public.FoodNutrition;
import com.zizy.healthexpert.R;

import java.util.ArrayList;

public class MaterialDetailActivity extends ActionBarActivity {
    listNutritionAdapter listAdapter;
    ArrayList<listNutritionAdapter.FoodNutrition > adapterList =new ArrayList<listNutritionAdapter.FoodNutrition >();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_material_detail);
        /*Get foodID from intent*/
        int foodID = getIntent().getIntExtra("foodID", 100);
        /*find views*/
        TextView txtFoodName = (TextView) findViewById(R.id.txtName);
        ListView listNutrition = (ListView) findViewById(R.id.listNutrition);
        /*prepare database*/
        MyDataBase_T_FOODS myDataBase_t_foods = new MyDataBase_T_FOODS(getApplicationContext());
        MyDataBase_T_NUTRITION myDataBase_t_nutrition = new MyDataBase_T_NUTRITION(getApplicationContext());
        MyDataBase_T_FOODS_TYPE myDataBase_t_foods_type = new MyDataBase_T_FOODS_TYPE(getApplicationContext());
        MyDataBase_T_FOODS_NUTRITION myDataBase_t_foods_nutrition = new MyDataBase_T_FOODS_NUTRITION(getApplicationContext());
        /*search the id and get a only result*/
        //this is a nutrition result
        com.zizy.healthexpert.Public.FoodNutrition foodNutrition;
            /*name*/
        String foodName = ((MyDataBase_T_FOODS.T_FOODS)
                myDataBase_t_foods.SearchT_FOODS("*", String.format("_ID='%s'", foodID)).get(0)).NAME;
            /*nutrition*/
        ArrayList<MyDataBase_T_FOODS_NUTRITION.T_FOODS_NUTRITION> foodNutritionList = myDataBase_t_foods_nutrition.SearchT_FOODS_NUTRITION("*", String.format("food_ID=%d", foodID));
        for (int index = 0; index < foodNutritionList.size(); index++) {
            adapterList.add(new listNutritionAdapter.FoodNutrition(
                    FoodNutrition.NutritionID2Name(getApplicationContext(), foodNutritionList.get(index).NUTRITION_ID),
                    foodNutritionList.get(index).CONENT_VALUE,
                    FoodNutrition.NutritionID2Unit(getApplicationContext(),  foodNutritionList.get(index).NUTRITION_ID)));

        }
        /*set adapterList */
        listAdapter=new listNutritionAdapter(getApplicationContext(),R.layout.list_nutrition,adapterList);
        /*set txtViews*/
        txtFoodName.setText(foodName);
        /*set listView*/
        listNutrition.setAdapter(listAdapter);


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.material, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
